package playing.action;

import object.Plant;
import playing.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class interaction {
    private int userId;
    private JFrame frame;
    private JTextField plantIdField;

    public interaction(int id) {
        this.userId = id;
        setupGUI();
    }

    private void setupGUI() {
        frame = new JFrame("植物互动界面");
        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        frame.add(panel);

        JLabel label = new JLabel("请输入你要互动的植物的ID：");
        panel.add(label);

        plantIdField = new JTextField(10);
        panel.add(plantIdField);

        JButton interactButton = new JButton("互动");
        panel.add(interactButton);

        interactButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int plantId = Integer.parseInt(plantIdField.getText());
                if (new PlayerPlantReading().getPlantIdsByUserId(userId).contains(plantId)) {
//                    System.out.println(plantId);
//                    for(int i:new PlayerPlantReading().getPlantIdsByUserId(userId))
//                        System.out.println(i);
                    openGrowthWindow(userId, plantId);
                } else {
                    JOptionPane.showMessageDialog(frame, "你输入的植物不存在于你的账号中，请重新输入。");
                }
            }
        });

        frame.setVisible(true);
    }

    private void openGrowthWindow(int userId, int plantId) {
        Plant plant = new PlantDataReader().getPlantById(plantId);

        JFrame growthFrame = new JFrame("植物成长互动");
        growthFrame.setSize(300, 200);
        growthFrame.setLocationRelativeTo(null);

        JPanel growthPanel = new JPanel();
        growthFrame.add(growthPanel);

        JLabel growthLabel = new JLabel("目前支持浇水和施肥功能");
        growthPanel.add(growthLabel);

        JButton waterButton = new JButton("浇水");
        growthPanel.add(waterButton);

        JButton fertilizeButton = new JButton("施肥");
        growthPanel.add(fertilizeButton);

        JButton backButton = new JButton("回到主菜单");
        growthPanel.add(backButton);

        waterButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                plant.setGrowthLevel(plant.getGrowthLevel() + 10);
                JOptionPane.showMessageDialog(growthFrame, "浇水成功");
                new PlantDataReader().savePlant(plant);
            }
        });

        fertilizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                plant.setGrowthLevel(plant.getGrowthLevel() + 40);
                JOptionPane.showMessageDialog(growthFrame, "施肥成功");
                new PlantDataReader().savePlant(plant);
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                growthFrame.dispose();
                frame.dispose();
            }
        });

        frame.setVisible(false); // 隐藏主界面
        growthFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new interaction(1); // 假设用户ID为1
            }
        });
    }
}
